/*
name名称
face头像
pf模型
plv武力
dlv统率
ilv智力
mlv政治
hlv体力
job职业
qua品质
cha性格
sex性别1--男0--女
skill技能数组0-4个被动&主动
ftype五行
lv等级
*/

window._NPC={}

window._NPCHELPER = {

    //各评级人数
    per:[100,180,240,290,330,23],

    //ilv,dlv,mlv,hlv
    importNpc:function(){
        var npcmap = _NPCHELPER.recreateNpc();
        for(var key in _NPC){
            if(npcmap[key]){
                _NPC[key].plv = npcmap[key][0];
                _NPC[key].ilv = npcmap[key][1];
                _NPC[key].dlv = npcmap[key][2];
                _NPC[key].mlv = npcmap[key][3];
                _NPC[key].hlv = npcmap[key][4];

                if(!_NPC[key].name){
                  _NPC[key].name=key;
                }
                if(!_NPC[key].face){
                  _NPC[key].face=key;
                }
                if(!_NPC[key].pf){
                  _NPC[key].pf=key;
                }

            }
        }

    },

    recreateNpc:function(){

       var perindex = 0;
       var totalnmb = 0;
       var reallevel = "D";
       var wnmb = 0;
       var dynmb = 0;
       var dxnmb = 0;
       var drnmb = 0;

        var base = 100;
        var add = 20;
        var addboss = 26;
        var npcmap = {};
    
        var bossarr = [];
        var npcarr = [];
        for(var key in _NPC){
          //console.log(key,_NPC[key]);
          var npcconf = _NPC[key];
          var total = npcconf.plv+npcconf.dlv+npcconf.ilv+npcconf.mlv+npcconf.hlv;
          if(npcconf.job!='无业' 
            &&npcconf.race!='mo'
            &&npcconf.race!='ji'
            &&npcconf.race!='shou'
            &&npcconf.race!='ling'){
            
            if(!npcconf.isboss){
              npcarr.push({name:key,total:total,conf:npcconf,reallevel:null});
            }else{
              bossarr.push({name:key,total:total,conf:npcconf,reallevel:null});
            }
          }
    
        }
    
        npcarr.sort((a, b) => {
          var lva = a.total;
          var lvb = b.total;
          return lva - lvb;
        });
    
        //console.log(npcarr);
    
        bossarr.sort((a, b) => {
          var lva = a.total;
          var lvb = b.total;
          return lva - lvb;
        });
    
        for(var i=0;i<npcarr.length;i++){
          var npcconf = _NPC[npcarr[i].name];
          var job = npcconf.job;
          var race = npcconf.race;
          //console.log(job,window._SKILLCOMBINE[job]);
          var attrper = window._SKILLCOMBINE[job]['五维比例'][race];
          var plv = Math.floor(attrper.plv/10*base);
          var ilv = Math.floor(attrper.ilv/10*base);
          var dlv = Math.floor(attrper.dlv/10*base);
          var mlv = Math.floor(attrper.mlv/10*base);
          var hlv = Math.floor(base-plv-ilv-dlv-mlv);
          
          npcmap[npcarr[i].name]=[plv,ilv,dlv,mlv,hlv];
          base+=add;

          totalnmb++;
          if(totalnmb>_NPCHELPER.per[perindex]){
            perindex++;
            if(reallevel=="D"){
              reallevel="C";
            }else if(reallevel=="C"){
              reallevel="B";
            }else if(reallevel=="B"){
              reallevel="A";
            }else if(reallevel=="A"){
              reallevel="S";
            }else if(reallevel=="S"){
              reallevel="SS";
            }
          }
          if(npcarr[i].conf.level!=reallevel){
            console.log(npcarr[i].name,npcarr[i].conf.level,reallevel,plv,ilv,dlv,mlv,hlv,(plv+ilv+dlv+mlv+hlv));
            wnmb++;
          }

          if(reallevel=="D"){
            if(npcarr[i].conf.race=="yao"){
              dynmb++;
            }else if(npcarr[i].conf.race=="xian"){
              dxnmb++;
            }else if(npcarr[i].conf.race=="ren"){
              drnmb++;
            }
          }
        }
    
        base=142;
    
        for(var i=0;i<bossarr.length;i++){
          var npcconf = _NPC[bossarr[i].name];
          var job = npcconf.job;
          var race = npcconf.race;
          var attrper = window._SKILLCOMBINE[job]['五维比例'][race];
          var plv = Math.floor(attrper.plv/10*base);
          var ilv = Math.floor(attrper.ilv/10*base);
          var dlv = Math.floor(attrper.dlv/10*base);
          var mlv = Math.floor(attrper.mlv/10*base);
          var hlv = base-plv-ilv-dlv-mlv;
          
          npcmap[bossarr[i].name]=[plv,ilv,dlv,mlv,hlv];
          base+=addboss;

          totalnmb++;
          if(totalnmb>_NPCHELPER.per[perindex]){
            perindex++;
            if(reallevel=="D"){
              reallevel="C";
            }else if(reallevel=="C"){
              reallevel="B";
            }else if(reallevel=="B"){
              reallevel="A";
            }else if(reallevel=="A"){
              reallevel="S";
            }else if(reallevel=="S"){
              reallevel="SS";
            }
          }
          if(bossarr[i].conf.level!=reallevel){
            wnmb++;
            console.log(bossarr[i].name,bossarr[i].conf.level,reallevel,plv,ilv,dlv,mlv,hlv,(plv+ilv+dlv+mlv+hlv));
          }

          if(reallevel=="D"){
            if(bossarr[i].conf.race=="yao"){
              dynmb++;
            }else if(bossarr[i].conf.race=="xian"){
              dxnmb++;
            }else if(bossarr[i].conf.race=="ren"){
              drnmb++;
            }
          }
        }
    
        console.log("总数:"+(npcarr.length+bossarr.length),wnmb);
        //console.log(npcmap);
        console.log("人"+drnmb+"  仙"+dxnmb+"  妖"+dynmb+"");
        return npcmap;
      },


      createTz:function(){

        var bossarr = [];
        var npcarr = [];
        for(var key in _NPC){
          var npcconf = _NPC[key];
          var total = npcconf.plv+npcconf.dlv+npcconf.ilv+npcconf.mlv+npcconf.hlv;
          if(npcconf.job!='无业'){
            if(!npcconf.isboss){
              npcarr.push({name:key,total:total,conf:npcconf});
            }else{
              bossarr.push({name:key,total:total,conf:npcconf});
            }
          }
    
        }
    
        bossarr.sort((a, b) => {
          var lva = a.total;
          var lvb = b.total;
          return lva - lvb;
        });

        var arr = [];
        var arr1,arr2;
        var index=0;
        for(var i=38;i<bossarr.length;i++){

          if(index==0){
            arr1=[];
            arr2=[];
            arr.push([arr1,arr2]);
          }
          var key = bossarr[i].name;

          if(index==0){
            for(var j=0;j<5;j++){
              arr1.push(key);
            }
          }else if(index==1){
            for(var j=0;j<5;j++){
              arr2.push(key);
            }
          }else if(index==2){
            for(var j=0;j<3;j++){
              arr1.push(key);
            }
          }else if(index==3){
            for(var j=0;j<3;j++){
              arr2.push(key);
            }
          }else if(index==4){
            for(var j=0;j<2;j++){
              arr1.push(key);
            }
          }else if(index==5){
            for(var j=0;j<2;j++){
              arr2.push(key);
            }
          }


          index++;
          if(index>5){
            index=0;
          }
        }

        //console.log(arr);
        for(var i=0;i<_CTZ.length;i++){
          _CTZ[i].player=arr[i][0];
          _CTZ[i].army=arr[i][1];
        }
      }
}